﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Collections;

namespace GMS.BIZ
{
    public class GuideStudents
    {
        private string teacherID;
        public string TeacherID
        {
            get { return teacherID; }
            //set { teacherID = value; }
        }

        /// <summary>
        /// 初始化函数
        /// </summary>
        /// <param name="teacherID">教师工号</param>
        public GuideStudents(string teacherID)
        {
            this.teacherID = teacherID;
        }

        /// <summary>
        /// 某年所带学生数量
        /// </summary>
        /// <param name="graduateYear">毕业年份</param>
        public int count(string graduateYear)
        {
            GMS.ORM.DAL.PGuide optPGuide = new GMS.ORM.DAL.PGuide();
            DataSet ds = optPGuide.GetList("gyear='" + graduateYear + "' AND tch_id='" + this.teacherID + "'");
            return ds.Tables[0].Rows.Count;
        }

        /// <summary>
        /// 本届所带学生数量
        /// </summary>
        public int countCurrentYear()
        {
            return this.count(DeadLine.GetDeadLineYear());
        }

        /// <summary>
        /// 为教师添加指导的学生
        /// </summary>
        /// <param name="studentID">学生学号</param>
        public string Add(string studentID)
        {
            //学生必须是本届的，year的问题全部采用DeadLine.GetDeadLineYear
            //-----------------------------------------
            //学生是否存在，PGuide记录是否存在
            GMS.ORM.DAL.PGuide optPGuide = new GMS.ORM.DAL.PGuide();
            if (!optPGuide.Exists(studentID, DeadLine.GetDeadLineYear()))
                return ErrorMessage.NoSuchData;

            //学生是否有指导老师，有指导老师了就不能添加
            //GMS.ORM.Model.PGuide myPGuide = new GMS.ORM.Model.PGuide();
            //myPGuide = optPGuide.GetModel(studentID, DeadLine.GetDeadLineYear());
            //if (myPGuide.tch_id != string.Empty)
            //    return ErrorMessage.AlreadyExitsData;

            //成功添加
            GMS.ORM.Model.PGuide myPGuide = new GMS.ORM.Model.PGuide();
            myPGuide = optPGuide.GetModel(studentID, DeadLine.GetDeadLineYear());
            myPGuide.tch_id = this.teacherID;
            bool ok = optPGuide.Update(myPGuide);
            if (!ok) return ErrorMessage.DBOptError;
            else return ErrorMessage.OK;
        }

        /// <summary>
        /// 放弃指导此学生
        /// </summary>
        /// <param name="studentID">学生学号</param>
        //public string Delete(string studentID)
        //{
        //    //学生必须是本届的，year的问题全部采用DeadLine.GetDeadLineYear
        //    //-----------------------------------------
        //    //学生是否存在，PGuide记录是否存在
        //    GMS.ORM.DAL.PGuide optPGuide = new GMS.ORM.DAL.PGuide();
        //    if (!optPGuide.Exists(studentID, DeadLine.GetDeadLineYear()))
        //        return ErrorMessage.NoSuchData;

        //    //学生是否有指导老师，没有指导老师就不能删除
        //    GMS.ORM.Model.PGuide myPGuide = new GMS.ORM.Model.PGuide();
        //    myPGuide = optPGuide.GetModel(studentID, DeadLine.GetDeadLineYear());
        //    if (myPGuide.tch_id == string.Empty)
        //        return ErrorMessage.NoSuchData;
            
        //    //指导老师是否是本人，不是本人不能删除
        //    if (myPGuide.tch_id != this.teacherID)
        //        return ErrorMessage.DataNotMatch;

        //    //删除指导的学生
        //    myPGuide.tch_id = string.Empty;
        //    bool ok = optPGuide.Update(myPGuide);
        //    if (!ok) return ErrorMessage.DBOptError;
        //    else return ErrorMessage.OK;
        //}

        /// <summary>
        /// 获得一名指导的学生
        /// </summary>
        /// <param name="studentID">学生学号</param>
        public Student GetOne(string studentID)
        {
            //学生必须是本届的，year的问题全部采用DeadLine.GetDeadLineYear
            //-----------------------------------------
            //学生是否存在，不存在返回null
            GMS.ORM.DAL.PGuide optPGuide = new GMS.ORM.DAL.PGuide();
            if (!optPGuide.Exists(studentID, DeadLine.GetDeadLineYear()))
                return null;
            
            //是否是自己的指导学生，不是自己的学生返回null
            if (optPGuide.GetModel(studentID, DeadLine.GetDeadLineYear()).tch_id != this.teacherID)
                return null;

            //返回这个学生的对象
            Student returnStudent = new Student(studentID);
            return returnStudent;
        }

        /// <summary>
        /// 获得本教师所有指导学生的列表
        /// </summary>
        /// <param name="graduateYear">毕业年份</param>
        public ArrayList GetList(string graduateYear)
        {
            ArrayList returnArray = new ArrayList();

            GMS.ORM.DAL.PGuide optPGuide = new GMS.ORM.DAL.PGuide();
            DataSet ds = optPGuide.GetList("tch_id='" + this.teacherID + "' AND gyear='" + graduateYear + "'");
            if (ds.Tables[0].Rows.Count == 0)
                return null;
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                returnArray.Add(ds.Tables[0].Rows[i]["std_id"].ToString());
            return returnArray;
        }

        /// <summary>
        /// 获得本教师当年所有指导学生的列表
        /// </summary>
        public ArrayList GetList()
        {
            return this.GetList(DeadLine.GetDeadLineYear());
        }
    }
}
